package edu.nyu.hps.assignment5;

public class Moves {
	
	int player;
	Square[] superGrid;
	
	public void createSquare(){
		int side = sideOfSquares();
		int i=side/2, j=side/2, noOfSquare = (500/side * 500/side);
		superGrid = new Square[noOfSquare];
		for(int k=0;k<superGrid.length;k++){
			superGrid[k] = new Square(side,Voronoi.grid[i][j]);
			if((i+side/2) >= 500 && (j+side)/2 >= 500){
				break;
			}
			if(j+side/2>=500){
				i+=side;
				j=side/2;
			} 
			else{
				j+=side;
			}
		}
	}

	public int sideOfSquares(){
		//if(Voronoi.time/Voronoi.stones < 5)
		//	return 50;
		//else
			return 10;
	}
	
	public void updateTaken(){
		int side = sideOfSquares(); 
		for(int i=0;i<superGrid.length;i++){
			for(int j = superGrid[i].center.x-(side/2);j<superGrid[i].center.x + (side/2);j++){
				for(int k= superGrid[i].center.y-(side/2);k<superGrid[i].center.y + (side/2);k++){
					if(Voronoi.grid[j][k].taken){
						superGrid[i].taken=true;
						superGrid[i].takenBy = Voronoi.grid[j][k].takenBy;
						superGrid[i].closestSquare = superGrid[i];
						break;
					}
				}
				if(superGrid[i].taken){
					break;
				}
			}
			if(!superGrid[i].taken){
				superGrid[i].takenBy = superGrid[i].center.takenBy;
			}
		}
	}

	public void updateClosestSquare(){
		for(int i=0;i<superGrid.length;i++){
			if(!superGrid[i].taken){
				double distToSquare;
				if(superGrid[i].closestSquare!=null){
					distToSquare = superGrid[i].center.dist(superGrid[i].closestSquare.center);
				}
				else{
					distToSquare = 251002;
				}
				for(int j=0;j<superGrid.length;j++){
					if(superGrid[j].taken && superGrid[i] != superGrid[j]){
						double distance = superGrid[i].center.dist(superGrid[j].center);
						if(distance < distToSquare){
							distToSquare = distance;
							superGrid[i].closestSquare = superGrid[j];
						}
					}
				}
			}
		}
	}
	public void setIValue(){
		for(int i=0;i<superGrid.length;i++){
			int x = superGrid[i].center.x;
			int y = superGrid[i].center.y;
			if((x>=83 && x<=418 && y<83) || 
				x>=83 && x<=125 && y>=83 && y<=125 ||
				x>=333 && x<=418 && y>=83 && y<=125 ||
				x>=83 && x<=418 && y>=33 && y<=418){
				superGrid[i].intrinsicValue = 1000;
			}
			else{
				superGrid[i].intrinsicValue = 500;
			}
		}
	}
	public double getNetRawScore(Square s){
		int myScore = 0,opponentScore = 0;
		double myWeightedScore=0.0, opponentWeightedScore=0.0;
		for(int i=0;i<superGrid.length;i++){
			if(!superGrid[i].taken && superGrid[i].takenBy != Voronoi.player){
				if(superGrid[i].center.dist(s.center) < superGrid[i].center.dist(superGrid[i].closestSquare.center)){
					myWeightedScore+=getNetIntrinsicValue(superGrid[i]);
					myScore+=1;
				}
			}
			else if(superGrid[i].takenBy == Voronoi.player){
				myWeightedScore+=getNetIntrinsicValue(superGrid[i]);
				myScore+=1;
			}
			else if(superGrid[i].takenBy == (Voronoi.player^1)){
				opponentWeightedScore+=getNetIntrinsicValue(superGrid[i]);
				opponentScore+=1;
			}
		}
		return (myWeightedScore-opponentWeightedScore);
	}
	
	public Point computeBestPoint(){
		updateTaken();
		updateClosestSquare();
		setIValue();
		double bestScore=0.0;
		Point bestPoint = null;
		for(int i=0;i<superGrid.length;i++){
			double tmpScore= getNetRawScore(superGrid[i]);
			if(bestScore<tmpScore){
				bestScore = tmpScore;
				bestPoint = superGrid[i].center;
			}
		}
		bestPoint.taken = true;
		bestPoint.takenBy = Voronoi.player;
		Voronoi.myStones.add(bestPoint);
		return bestPoint;
	}
	public double getNetIntrinsicValue(Square s){
		double dist = s.center.dist(s.closestSquare.center);
		return (double)s.intrinsicValue*dist;
//		return s.intrinsicValue;
	}
}
